<template>
  <div class="userlist_page page-container">
    <breadcrumb-com text="项目列表"></breadcrumb-com>
      <el-button @click="adduser()" type="success" size="small">添加项目</el-button>
      <el-button @click="deleSelect()" type="danger" size="small">删除选中</el-button>
     <el-table
      :data="tableData"
      height="650"
      border
      @selection-change="handleSelectionChange"
      style="width: 100%">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="date"
        label="日期"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="项目名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="desc"
        label="项目描述">
      </el-table-column>
          <el-table-column
          fixed="right"
          label="操作"
          width="200">
          <template slot-scope="scope">
            <el-button @click="editItem(scope.row.id)" type="primary" size="small" disabled>编辑</el-button>
            <el-button @click="deleItem(scope.row.id)" type="danger" size="small">删除</el-button>
          </template>
        </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      @current-change="pageChange"
      :total="count">
    </el-pagination>
  </div>

</template>

<script>

  export default {
    data() {
      return {
        tableData: [
          { id: 1,
            date: '2016-05-01',
            name: '王小虎',
            desc: '王小虎啊王小虎，王小龙啊王小龙'
          }
        ],
        count: 10,
        multipleSelection: [], 
      }
    },
    methods: {
      async pageChange(page) {
        let _this = this
        console.log(page)
        console.log(_this.multipleSelection)
      },
      handleClick(id) {
        console.log(id)
      },
      // 全选
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      // 删除选中
      deleSelect() {
        let _this = this
        _this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then( async () => {  // 确定删除
          // 获取要删除的id
          const arr = await _this.selectID()
          if(arr.length < 1){
            // 未选中项目
            _this.$message({
              type: 'info',
              message: '请选择删除项!',
            });
          } else {
            // 发送请求，请求成功，循环删除页面数据
            for(let i = 0; i < arr.length; i++) {
              let rowList = _this.tableData
              for(let j = 0; j< rowList.length; j++) {
                if(arr[i] == rowList[j].id) {
                  _this.tableData.splice(j,1)
                }
              }
            }
            // 删除成功提示消息
            _this.$message({
              type: 'success',
              message: '删除成功!',
            });
          }
        }).catch(() => {
          _this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
        
      },
      // 单个删除
      deleItem(id) {
        let _this = this
        _this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then( async () => {  // 确定删除
          // 获取要删除的id
          // 发送请求，请求成功，id查找对应数据，页面删除
          let rowList = _this.tableData
          for(let j = 0; j< rowList.length; j++) {
            if(id == rowList[j].id) {
              _this.tableData.splice(j,1)
            }
          }
          // 删除成功提示消息
          _this.$message({
            type: 'success',
            message: '删除成功!',
          });
        }).catch(() => {
          _this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      // 添加新用户
      adduser() {
        this.$router.push({
          path: '/userpost'
        })
      },
      // 编辑用户信息
      editItem(id) {
        console.log(id)
        this.$router.push({
          path: '/userpost',
          query: { // params
            id: id
          }
        })
      },
      // 处理选中
      selectID() {
        let _this = this
        let arr = []
        for(let i = 0; i < _this.multipleSelection.length; i++) {
          arr.push(_this.multipleSelection[i].id)
        }
        return arr
      }
    },
    watch: {
      count () {
        return _this.tableData.length
      }
    }
  }
</script>